<template>
     <div class="sidebar" :class="{ collapsed: isCollapsed }">
          <div class="sidebar-header">
               <div class="sidebar-title" v-show="!isCollapsed">
                    {{ activedHeader?.meta.title }}
               </div>
               <div class="collapse-btn" @click="toggleCollapse">
                    <i
                         :class="
                              isCollapsed
                                   ? 'iconfont icon-zhankai'
                                   : 'iconfont icon-shouqi-copy'
                         "
                    ></i>
               </div>
          </div>

          <div class="sidebar-menu">
               <el-tooltip
                    effect="light"
                    v-for="item in menuList"
                    :key="item.path"
                    :content="item.meta?.title"
                    placement="right"
                    :show-after="500"
               >
                    <div
                         class="menu-item"
                         :class="{
                              'is-active': isActiveRoute(item),
                         }"
                         @click="handleMenuClick(item)"
                    >
                         <div class="menu-icon">
                              <i
                                   v-if="item.meta?.icon"
                                   :class="item.meta.icon"
                              ></i>
                              <i v-else class="iconfont icon-menu"></i>
                         </div>
                         <div class="menu-content" v-show="!isCollapsed">
                              <div class="menu-title">
                                   {{ item.meta?.title }}
                              </div>
                         </div>
                    </div>
               </el-tooltip>
          </div>
     </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const props = defineProps({
     menuList: {
          type: Array as () => any[],
          default: () => [],
     },
     collapsed: {
          type: Boolean,
          default: false,
     },
})

// 判断当前路由是否有子集 如果有则显示侧边栏 没有的话则不显示
const menuList = computed(() => {
     const activeRoute = router.currentRoute.value
     const sideBarList = activeRoute.matched
          .map((item: any) => {
               if (
                    item.children.find((child: any) => {
                         return activeRoute.path == child.path
                    })
               ) {
                    return item.children
               }
          })
          .flat()
          .filter((item: any) => item)
     return sideBarList || []
})
const activedHeader = computed(() => {
     const activeRoute = router.currentRoute.value
     const header = activeRoute.matched.find((item: any) => {
          return activeRoute.path == item.path
     })
     return header
})

const emit = defineEmits(['update:collapsed'])

const route = useRoute()
const router = useRouter()

// 折叠状态
const isCollapsed = computed({
     get: () => props.collapsed,
     set: (value) => emit('update:collapsed', value),
})

// 判断当前路由是否激活
const isActiveRoute = (routeItem: RouteRecordRaw) => {
     return (
          route.path === routeItem.path ||
          route.path.startsWith(routeItem.path + '/')
     )
}

// 处理菜单点击
const handleMenuClick = (routeItem: RouteRecordRaw) => {
     router.push(routeItem.path)
}

// 切换折叠状态
const toggleCollapse = () => {
     isCollapsed.value = !isCollapsed.value
}
</script>

<style lang="scss" scoped>
.sidebar {
     // 调整为白色系背景
     --sidebar-bg: #ffffff;
     --sidebar-border: #e0e0e0;
     --text-primary: #54a4ea;
     --text-secondary: #666666;
     --accent-color: #54a4ea;
     --accent-color-transparent: #54a4ea20;
     --accent-hover: #4293da;
     --active-bg: #54a4ea15;
     --hover-bg: #f5f7fa;
     --icon-color: #54a4ea;
     width: 100%;
     height: 100%;
     box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
     display: flex;
     flex-direction: column;
     overflow: hidden;
     border-radius: 8px;
     transition: all 0.3s ease;

     i {
          font-size: 18px;
          color: var(--icon-color);
          transition: all 0.3s ease;
     }

     &.collapsed {
          .sidebar-header {
               justify-content: center !important;
          }
     }

     .sidebar-header {
          padding: 12px 16px;
          border-bottom: 1px solid var(--sidebar-border);
          display: flex;
          align-items: center;
          justify-content: space-between;
          transition: all 0.3s ease;

          .sidebar-title {
               font-size: 16px;
               font-weight: 600;
               letter-spacing: 0.5px;
               color: var(--text-primary);
               white-space: nowrap;
               transition: all 0.3s ease;
          }

          .collapse-btn {
               padding: 5px;
               display: flex;
               align-items: center;
               justify-content: center;
               border-radius: 6px;
               cursor: pointer;
               transition: all 0.3s ease;
               background: var(--hover-bg);
               border: 1px solid transparent;

               &:hover {
                    background: var(--accent-color);
                    transform: scale(1.1);
                    box-shadow: 0 2px 6px rgba(84, 164, 234, 0.3);
               }

               &:hover i {
                    color: #ffffff;
               }

               i {
                    font-size: 18px;
                    color: var(--icon-color);
                    transition: all 0.3s ease;
               }
          }
     }

     .sidebar-menu {
          padding: 8px;
          overflow-y: auto;
          display: flex;
          gap: 6px;
          flex-direction: column;
          flex: 1;

          // 自定义滚动条样式
          &::-webkit-scrollbar {
               width: 6px;
          }

          &::-webkit-scrollbar-track {
               background: #f1f1f1;
               border-radius: 3px;
          }

          &::-webkit-scrollbar-thumb {
               background: #c1c1c1;
               border-radius: 3px;

               &:hover {
                    background: #a8a8a8;
               }
          }

          .menu-item {
               display: flex;
               align-items: center;
               position: relative;
               border-radius: 8px;
               transition: all 0.3s ease;
               cursor: pointer;
               overflow: hidden;
               padding: 8px;
               gap: 10px;
               background: #ffffff;
               border: 1px solid transparent;
               box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);

               &:last-child {
                    margin-bottom: 0;
               }

               .menu-icon {
                    width: 25px;
                    height: 25px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    flex-shrink: 0;
                    border-radius: 6px;
                    transition: all 0.3s ease;
                    background-color: transparent;
               }

               &:hover {
                    background: var(--hover-bg);
                    transform: translateX(3px);
                    box-shadow: 0 2px 8px rgba(84, 164, 234, 0.15);
                    border-color: rgba(84, 164, 234, 0.3);
               }

               &.is-active {
                    border: 1px solid var(--accent-color);
                    background: var(--active-bg);
                    box-shadow: 0 3px 10px rgba(84, 164, 234, 0.2);

                    // 激活状态的左侧装饰线
                    &::before {
                         content: '';
                         position: absolute;
                         left: 0;
                         top: 50%;
                         transform: translateY(-50%);
                         width: 3px;
                         height: 60%;
                         background: var(--accent-color);
                         border-radius: 0 4px 4px 0;
                    }

                    .menu-title {
                         color: var(--accent-color);
                         font-weight: 600;
                    }

                    .menu-icon {
                         color: var(--accent-color);

                         i {
                              font-size: 18px;
                              color: var(--accent-color);
                              transform: scale(1.1);
                         }
                    }
               }

               .menu-content {
                    display: flex;
                    flex-direction: column;
                    gap: 2px;
                    max-width: calc(100% - 40px);

                    .menu-title {
                         font-size: 14px;
                         color: #333333;
                         transition: all 0.3s ease;
                         font-weight: 500;
                         white-space: nowrap;
                         overflow: hidden;
                         text-overflow: ellipsis;
                    }

                    .menu-description {
                         font-size: 12px;
                         color: var(--text-secondary);
                         margin-top: 2px;
                    }
               }
          }
     }
}

// 响应式设计
@media screen and (max-width: 768px) {
     .sidebar {
          border-radius: 0;
          box-shadow: none;

          .sidebar-header {
               padding: 16px;

               .sidebar-title {
                    font-size: 14px;
               }
          }

          .sidebar-menu {
               padding: 4px;

               .menu-item {
                    padding: 10px 12px;

                    .menu-content {
                         .menu-title {
                              font-size: 14px;
                         }
                    }
               }
          }
     }
}

// 滚动条样式优化
.sidebar-menu::-webkit-scrollbar {
     width: 5px;
}

.sidebar-menu::-webkit-scrollbar-track {
     background: #f1f1f1;
     border-radius: 10px;
}

.sidebar-menu::-webkit-scrollbar-thumb {
     background: #c1c1c1;
     border-radius: 10px;
}

.sidebar-menu::-webkit-scrollbar-thumb:hover {
     background: var(--accent-color);
}

// 动画效果
@keyframes fadeIn {
     from {
          opacity: 0;
          transform: translateY(-10px);
     }

     to {
          opacity: 1;
          transform: translateY(0);
     }
}

@keyframes pulse {
     0% {
          box-shadow: 0 0 0 0 rgba(84, 164, 234, 0.4);
     }
     70% {
          box-shadow: 0 0 0 10px rgba(84, 164, 234, 0);
     }
     100% {
          box-shadow: 0 0 0 0 rgba(84, 164, 234, 0);
     }
}

.sidebar-menu {
     .menu-item {
          animation: fadeIn 0.3s ease-out;
     }

     .menu-item.is-active {
          animation:
               fadeIn 0.3s ease-out,
               pulse 2s infinite;
     }
}
</style>
